<?php

include_once("interface.php");

class Access extends Data
{
    private $fn = "/etc/lighttpd/lighttpd.conf";
    private $fn2 = "/etc/ssh/sshd_config";
    private $fn3 = "/etc/lighttpd/lighttpd.users";
    public $title = "Remote Access";
    public $module = "access";
    public $data;

    public function __construct() {
        $this->data = array("nmsport" => 0,
                            "sshport" => 0,
                            "webpassword" => ""
                            );
    }

    //read the data from files and return a complete object
    function readDataFromSystem() {

        //get nms port
        $filearray = file($this->fn);
        if ( $filearray == false ) {
            return false;
        }
        foreach ( $filearray as $arrval ) {
            if(preg_match('/server.port.*/', $arrval)) {
                $tmp = preg_split('/=/', $arrval);
                $this->data['nmsport'] = trim($tmp[1]);       
                break;
            }
        }

        //get ssh port
        $filearray = file($this->fn2);
        if ( $filearray == false ) {
            return false;
        }
        foreach ( $filearray as $arrval ) {
            if(preg_match('/^[#]{0,1}Port.*/', $arrval)) {
                $tmp = preg_split('/ /', $arrval);
                $this->data['sshport'] = trim($tmp[1]);       
                break;
            }
        }

        error_log("RemoteAccess readDataFromSystem(): read from $this->fn");
        return $this->data;
    }

    //write the obj to the file
    function commitDataToSystem($obj) {
        $ret;
        $retstr = "Successfully set the access ports.";
        $out = array();
        error_log("RemoteAccess commitDataToSystem(): writing, nmsport=".$obj['nmsport'].", sshport=".$obj['sshport']);
        $cmd = "sed -i -r 's/server.port.*/server.port=".$obj['nmsport']."/' ".$this->fn;
        exec($cmd, &$out, &$ret); 
        if($ret != 0) {
            $retstr = "Fail to set Web RemoteAccess port";
        }
        $cmd = "sed -i -r 's/^[#]{0,1}Port.*/Port ".$obj['sshport']."/' ".$this->fn2;
        exec($cmd, &$out, &$ret); 
        if($ret != 0) {
            $retstr = "Fail to set SSH port";
        }

        $cmd = "/etc/init.d/lighttpd restart";
        exec($cmd, &$out, &$ret);
        if($ret != 0) {
            $retstr = "Could not restart lighttpd server.";
        }

        //change password if set
        if($obj['webpassword'] != "") {
            $retstr = "Successfully set the access ports and changed the admin password";
            $fd = fopen($this->fn3, "w+");
            $ret = fwrite($fd, "admin:".$obj['webpassword']);
            if($ret == false) {
                $retstr = "Fail to set Admin password";
            }
            fclose($fd);
        }
        return $retstr;
    }

    function printModuleHTML() {
        $str = <<<END

    <form>
    <fieldset class="ui-widget-content">
    <legend>Web Access</legend>
    <br>
    SSH Port: <input id="sshport" type=text size=5 /><br>
    <br>
    Web Access Port: <input id="nmsport" type=text size=5 /><br>
    <br>
    New Admin Password: <input id="webpassword" type=password size=15 /><br>
    <br>
    Re-Enter Password: <input id="webpassword1" type=password size=15 />
    </fieldset>
    </form>
    <br>
    <button id="submit" class="ui-widget-button">Submit</button>
    <div id="info"></div>

END;

        return $str;
    }

    function printModuleScript() {
        $str = <<<END

<script type="text/javascript">

    //fill in the form using values received from server
    function writeDataToHTML(data) {
        $('#sshport').val(data.sshport);
        $('#nmsport').val(data.nmsport);
    }

    //fill in the obj to be sent to the server using the form values
    function readDataFromHTML() {
        var data = Object();
        data.sshport = $('#sshport').val();
        data.nmsport = $('#nmsport').val();
        data.webpassword = $('#webpassword').val();
        data.webpassword1 = $('#webpassword1').val();
        return data;
    }

    function validate(obj) {
        var msg = null;
        if(obj.sshport < 0 || obj.sshport > 65535) {
            msg = "SSH port needs to be between 0-65535"
        }
        if(obj.nmsport < 0 || obj.nmsport > 65535) {
            msg = "Web access port needs to be between 0-65535"
        }
        if(obj.nmsport == obj.sshport) {
            msg = "Web access and SSH port cannot be the same"
        }
        if(obj.webpassword !== obj.webpassword1) {
            msg = "Password did not match"
        }
        return msg;
    }

    function beforeSendFunction(msg) {
        $("#submit").html("Sending ... ");
    }

    //called when the submit operation succeeds
    function successFunction(msg) {
        $("#submit").html("Submit");
        $("#info").html("Success: " + msg);
    }

    //called when the submit operation fails
    function errorFunction(msg) {
        $("#info").html("Error: " + msg);
    }

</script>

END;
        return $str;
    }

}

?>
